package com.gkzf.ai.module.crm.config;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.gkzf.ai.framework.common.exception.ErrorCode;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import static com.gkzf.ai.framework.common.exception.util.ServiceExceptionUtil.exception;


/**
 * @author Huawei
 * @date 2025-04-01 15:34
 */
public abstract class BaseService<T> {

    @Autowired
    private BaseMapper<T> repository;



    /**
     *  组合判断
     *  新增判断唯一值是否存在
     *  注意：keys 和 value 数据顺序对应
     * @param keys 组合key
     * @param value 组合值
     * @return
     */
    public void isFieldRepetition(List<SFunction<T, Object>> keys, List<Object> value, ErrorCode PRODUCT_NO_EXISTS) {
        // 动态构建查询条件（假设你有基于字段查询的需求）
        LambdaQueryWrapper<T> queryWrapper = new LambdaQueryWrapper<>();
        AtomicInteger index = new AtomicInteger(0);
        keys.forEach(key -> {
            queryWrapper.eq(key,value.get(index.get()));
            index.incrementAndGet();
        });
        List<T> ts = repository.selectList(queryWrapper);
        if (ts.isEmpty()){
            return;
        }
        throw exception(PRODUCT_NO_EXISTS);
    }

    /**
     *  新增判断唯一值是否存在
     *  判断数据库中某个字段值是否存在
     * @param key 对应key
     * @param value 值
     * @return
     */
    public void isFieldRepetition(SFunction<T, Object> key, Object value, ErrorCode PRODUCT_NO_EXISTS) {
        // 动态构建查询条件（假设你有基于字段查询的需求）
        LambdaQueryWrapper<T> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(key, value);
        List<T> ts = repository.selectList(queryWrapper);
        if (ts.isEmpty()){
            return;
        }
        throw exception(PRODUCT_NO_EXISTS);
    }

    /**
     *  编辑判断唯一值是否存在 并且 输入的Id 不等于 已存在的Id
     *  判断数据库中某个字段值是否存在
     * @param key 对应key
     * @param value 值
     * @return
     */
    public void isFieldRepetition(SFunction<T, Object> key, Object value, SFunction<T, Object> idKey, Object idValue,ErrorCode PRODUCT_NO_EXISTS) {
        // 动态构建查询条件（假设你有基于字段查询的需求）
        LambdaQueryWrapper<T> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(key, value);
        queryWrapper.ne(idKey, idValue);
        List<T> ts = repository.selectList(queryWrapper);
        if (ts.isEmpty()){
           return;
        }
        throw exception(PRODUCT_NO_EXISTS);
    }


    /**
     *  判断ID是否存在 如果存在则返回Id对象  如不返回则报错
     * @param idKey id
     * @return
     */
    public T isIdExists(Serializable idKey, ErrorCode PRODUCT_NO_EXISTS) {
        T selectById = repository.selectById(idKey);
        if (null != selectById){
            return selectById;
        }
        throw exception(PRODUCT_NO_EXISTS);
    }

    /**
     *  判断字段是否存在 如果存在则返回Id对象  如不返回则报错
     * @param key id
     * @return
     */
    public T isByFieldExists(SFunction<T, Object> key, Object value, ErrorCode PRODUCT_NO_EXISTS) {
        LambdaQueryWrapper<T> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(key, value);
        List<T> ts = repository.selectList(queryWrapper);
        if (ts.isEmpty()){
            throw exception(PRODUCT_NO_EXISTS);
        }
       return ts.get(0);
    }

    public List<T> getInList(SFunction<T, Object> key,Collection<Long> params){
        LambdaQueryWrapper<T> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(key,params);
        return repository.selectList(queryWrapper);
    }
}
